// /////////////////////////////////////////////////////////////////////////////
//
// Name:            ScriptedEvent.cpp
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           Internal class for allowing AngelScript to interface with
//                  the event system.
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

// Include files
#include "ScriptedEvent.h"

#ifdef __COMPILE_WITH_ANGELSCRIPT__

#include "../../core/GameManager.h"


// ScriptedEvent class
// ScriptedEvent constructor (EventManager).
ScriptedEvent::ScriptedEvent(asIScriptObject *object, const std::string &function)
: mObject(object), mFunction(function)
{
    // Set up pointers which will be used.
    mObject->AddRef();
    mContext = GameManager::Instance()->getScriptManager()->getEngine()->CreateContext();
}

// ScriptedEvent deconstructor.
ScriptedEvent::~ScriptedEvent()
{
    // Release the object.
    mObject->Release();
}

// Handles the event.
void ScriptedEvent::onEvent(void *p)
{
    int id = mObject->GetObjectType()->GetMethodIdByName(mFunction.c_str());

    mContext->Prepare(id);
    mContext->SetObject(mObject);
    mContext->SetArgAddress(0, p);
    mContext->Execute();
}

// Handles disconnects.
void ScriptedEvent::onDisconnect(void *p)
{
    drop();
}

#endif // __COMPILE_WITH_ANGELSCRIPT__

// End of File
